﻿using System;
using System.Collections.Generic;
using TheBlog.NhibernateModel;
using TheBlog.NhibernateModel.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate.Envers;

namespace TheBlog.Tests
{
    [TestClass]
    public class NhibernateInitializerDataTest
    {
        private NhibernateInitializer _initializer;

        public NhibernateInitializerDataTest()
        {
            _initializer = new NhibernateInitializer();
        }
        [TestMethod]
        public void CreateDatabase()
        {
            var startTime = DateTime.Now;
            _initializer = new NhibernateInitializer();
            var session = _initializer.CreateSessionFactoryWithAutomaticDbCreate().OpenSession();

            using (var tr = session.BeginTransaction())
            {
                var mesut = new User()
                {
                    UserName = "mesut",
                    Password = "123456"
                };
                session.Save(mesut);
                var sulhadin = new User()
                {
                    UserName = "sulhadin",
                    Password = "123456"
                };
                session.Save(sulhadin);
                Console.WriteLine("Kullanıcılar Oluşturuldu.");

                var dislikeAffectMesutToSulhadin = new UserAffect
                {
                    AffectingUser = mesut,
                    AffectedUser = sulhadin,
                    AffectingDate = DateTime.Now,
                    AffectingType = AffectingType.Dislike
                };
                session.Save(dislikeAffectMesutToSulhadin);
                var shareAffectMesutToSulhadin = new UserAffect
                {
                    AffectingUser = mesut,
                    AffectedUser = sulhadin,
                    AffectingDate = DateTime.Now,
                    AffectingType = AffectingType.Share
                };
                session.Save(shareAffectMesutToSulhadin);
                var likeAffectSulhadinToMesut = new UserAffect
                {
                    AffectingUser = sulhadin,
                    AffectedUser = mesut,
                    AffectingDate = DateTime.Now,
                    AffectingType = AffectingType.Like
                };
                session.Save(likeAffectSulhadinToMesut);
                Console.WriteLine("Kullanıcılar Beğenisi yapıldı.");

                var article = new Article
                {
                    Title = "Deneme 1",
                    Content = "<br />"
                };

                article.Comments.Add(new Comment
                {
                    CommentDate = DateTime.Now,
                    CommentText = "test",
                    FullName = "MCAKIR",
                   
                });
                session.Save(article);

                tr.Commit();
            }
            var diffDate = (DateTime.Now - startTime).TotalSeconds;
            Console.WriteLine(diffDate);
        }

        [TestMethod]
        public void Must_Open_Session_Time_Less_Than_00_4_Sec()
        {
            var startTime = DateTime.Now;
            _initializer.CreateSessionFactory();
            var diffDate = (DateTime.Now - startTime).TotalSeconds;
            Console.WriteLine(diffDate);
            Assert.IsTrue(diffDate < 0.04);
        }
        [TestMethod]
        public void Must_Create_Session_Factory_Less_Than_00_4_Sec_Than_Slowly()
        {
            var sessionfacttory = _initializer.CreateSessionFactory();
            var startTime = DateTime.Now;
            sessionfacttory.OpenSession();
            var diffDate = (DateTime.Now - startTime).TotalSeconds;
            Console.WriteLine(diffDate);
            Assert.IsTrue(diffDate < 0.04);
        }
    }
}
